package com.winit.params.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.base.bean.ExecuteResult;
import com.winit.commons.util.SeqGenerate;
import com.winit.config.model.Config;
import com.winit.config.service.IConfigService;
import com.winit.params.mapper.ParamsMapper;
import com.winit.params.model.Params;
import com.winit.params.service.ParamsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wanglin on 2018/6/4.
 */
@Service
public class ParamsServiceImpl implements ParamsService {

    private static Logger logger = LoggerFactory.getLogger(ParamsServiceImpl.class);

    @Autowired
    ParamsMapper paramsMapper;
    @Autowired
    IConfigService configService;

    @Override
    public ExecuteResult saveParams(Params params) {
        ExecuteResult er=new ExecuteResult();
        if(params==null){
            er.setCode("1");
            er.setDesc("入参不能为空");
        }
        params.setId(SeqGenerate.createId("PARA"));
        Config paramConfig=new Config();
        Config rtnConfig=new Config();
        if(params.getParamJson()!=null){
            paramConfig=saveParamConfig(params.getParamJson().toJSONString());
        }
        if(params.getRtn()!=null){
            rtnConfig= savcRtnConfig(params.getRtn().toJSONString());
        }
        params.setParamConfigId(paramConfig.getId());
        params.setRtnConfigId(rtnConfig.getId());
        try {
            params.setCreateDate(new Date());
            paramsMapper.insertSelective(params);
        }catch (Exception e){
            er.setCode("1");
            er.setDesc("保存异常，"+e.getMessage()+"请联系beetle管理员");
        }
        er.setData(params);
        return er;
    }
    
    @Override
    public ExecuteResult updateParams(Params params){
        ExecuteResult er=new ExecuteResult();
        if(params==null){
            er.setCode("1");
            er.setDesc("入参不能为空");
        }
        Params old = paramsMapper.selectByPrimaryKey(params.getId());
        if(!StringUtils.isEmpty(old.getParamConfigId())){
            configService.deleteConfigById(old.getParamConfigId());
        }
        if(!StringUtils.isEmpty(old.getRtnConfigId())){
            configService.deleteConfigById(old.getRtnConfigId());
        }
        Config paramConfig=new Config();
        Config rtnConfig=new Config();
        if(params.getParamJson()!=null){
            paramConfig=saveParamConfig(params.getParamJson().toJSONString());
        }
        if(params.getRtn()!=null){
            rtnConfig= savcRtnConfig(params.getRtn().toJSONString());
        }
        params.setParamConfigId(paramConfig.getId());
        params.setRtnConfigId(rtnConfig.getId());
        try {
            paramsMapper.updateByPrimaryKeySelective(params);
            er.setData(params);
        }catch (Exception e){
            er.setCode("1");
            er.setDesc("保存异常，"+e.getMessage()+"请联系beetle管理员");
        }
        return er;
    }

    @Override
    public ExecuteResult getParams(String relateId) {
        ExecuteResult er=new ExecuteResult();
        Map selectMap=new HashMap();
        selectMap.put("relateId",relateId);
        List<Params> paramList = paramsMapper.getRelateParams(selectMap);
        for(Params param:paramList){
            try {
                Config paramConfig = configService.getConfigMap(param.getParamConfigId(), false);
                param.setParamJson(JSONObject.parseObject(paramConfig.getKVMap().get("paramJson")));
            } catch (Exception e) {
                er.setDesc(param.getName()+"查询参数异常"+e.getMessage());
                return er;
            }
            try {
                Config rtnConfig = configService.getConfigMap(param.getRtnConfigId(), false);
                param.setRtn(JSONObject.parseObject(rtnConfig.getKVMap().get("rtn")));
            }catch (Exception e){
                er.setDesc(param.getName()+"查询返回值异常"+e.getMessage());
                return er;
            }
        }
        er.setData(paramList);
        return er;
    }

    @Override
    public ExecuteResult deleteParams(String id) {
        ExecuteResult er=new ExecuteResult();

        Params params = paramsMapper.selectByPrimaryKey(id);

        if(null==params){
            er.setCode("1");
            er.setDesc("参数信息不存在！");
            return er;
        }
        try{
            paramsMapper.deleteByPrimaryKey(id);
        }catch (Exception e){
            er.setCode("1");
            er.setDesc("删除失败，请联系beetle管理员");
            logger.error(e.getMessage());
            return er;
        }
        return er;
    }



    private Config saveParamConfig(String json){
        Config paramConfig=new Config();
        Map paramMap = new HashMap<>();
        paramMap.put("paramJson",json);
        paramConfig.setKVMap(paramMap);
        configService.saveConfig(paramConfig);
        return paramConfig;
    }


    private Config savcRtnConfig(String json){
        Config rtnConfig=new Config();
        Map paramMap = new HashMap<>();
        paramMap.put("rtn",json);
        rtnConfig.setKVMap(paramMap);
        configService.saveConfig(rtnConfig);
        return rtnConfig;
    }
    
    
}
